// pages/home-video/index.js
import { getTopMVs } from '../../service/api_video'

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		topMVs: [],
		hasMore: true
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getMVs(0)
	},

	getMVs: async function (offset) {
		if (!this.data.hasMore && offset !== 0) return
		wx.showNavigationBarLoading()
		const res = await getTopMVs(offset)
		let newData = this.data.topMVs
		if (offset === 0) {
			newData = res.data
		} else {
			newData = newData.concat(res.data)
		}
		this.setData({ topMVs: newData })
		this.setData({ hasMore: res.hasMore })
		wx.hideNavigationBarLoading()
		if (offset === 0) {
			wx.stopPullDownRefresh()
		}
	},

	toVideoDetail: function (event) {
		const id = event.currentTarget.dataset.item.id
		wx.navigateTo({
			url: `../video-content/index?id=${id}`
		})
	},

	onPullDownRefresh: function () {
		this.getMVs(0)
	},

	onReachBottom: function () {
		this.getMVs(this.data.topMVs.length)
	}
})
